//
//  DetailsCentroideViewController.m
//  PracticoDAW
//
//  Created by Juan Jose Alcazar Cobano on 30/05/13.
//  Copyright (c) 2013 Juan Jose Alcazar Cobano. All rights reserved.
//

#import "DetailsCentroideViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "DejalActivityView.h"
#import "MapViewAnnotation.h"
#import <CoreLocation/CoreLocation.h>

@interface DetailsCentroideViewController ()

@end

@implementation DetailsCentroideViewController

@synthesize centroide;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        UINavigationItem *navigationItem = [self navigationItem];
        [navigationItem setTitle:@"Detalles"];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
        
    [[map layer] setCornerRadius:5.0];
    [[map layer] setMasksToBounds:YES];
    [[map layer] setBorderWidth:0.1];
    
    [DejalBezelActivityView activityViewForView:[[self navigationController] view]];
    
    CLLocation *locationLatitude = [[CLLocation alloc] initWithLatitude:[[centroide latitude] floatValue] longitude:[[centroide longitude] floatValue]];
    NSLog(@"%@", locationLatitude);
    
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder reverseGeocodeLocation:locationLatitude completionHandler:^(NSArray *placemarks, NSError *error) {
        CLPlacemark *placemark = [placemarks objectAtIndex:0];
        NSLog(@"%@", [placemark addressDictionary]);
        
        //NSString *locatedAt = [[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];
        NSString *street = [placemark.addressDictionary valueForKey:@"Street"];
        NSString *city = [placemark.addressDictionary valueForKey:@"City"];
        
        [addressLabel setText:[NSString stringWithFormat:@"Dirección: %@, %@", street, city]];
        
        [DejalBezelActivityView removeView];
    }];
    
    CLLocationCoordinate2D location;
	location.latitude = [[centroide latitude] floatValue];
	location.longitude = [[centroide longitude] floatValue];
    
	// Add the annotation to our map view
    NSString *visits = [NSString stringWithFormat:@"Has estado %@ veces", [centroide numVeces]];
	MapViewAnnotation *newAnnotation = [[MapViewAnnotation alloc] initWithTitle:visits andCoordinate:location];
	[map addAnnotation:newAnnotation];
}

- (void)mapView:(MKMapView *)mv didAddAnnotationViews:(NSArray *)views
{
	MKAnnotationView *annotationView = [views objectAtIndex:0];
	id <MKAnnotation> mp = [annotationView annotation];
	MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance([mp coordinate], 1500, 1500);
	[mv setRegion:region animated:YES];
	[mv selectAnnotation:mp animated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
